﻿// Decompiled with JetBrains decompiler
// Type: NanoSockets.UDP
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;

#nullable disable
namespace NanoSockets;

[SuppressUnmanagedCodeSecurity]
public static class UDP
{
  private const string nativeLibrary = "nanosockets";
  public const int hostNameSize = 1025;

  [DllImport("nanosockets", EntryPoint = "nanosockets_initialize", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status Initialize();

  [DllImport("nanosockets", EntryPoint = "nanosockets_deinitialize", CallingConvention = CallingConvention.Cdecl)]
  public static extern void Deinitialize();

  [DllImport("nanosockets", EntryPoint = "nanosockets_create", CallingConvention = CallingConvention.Cdecl)]
  public static extern Socket Create(int sendBufferSize, int receiveBufferSize);

  [DllImport("nanosockets", EntryPoint = "nanosockets_destroy", CallingConvention = CallingConvention.Cdecl)]
  public static extern void Destroy(ref Socket socket);

  [DllImport("nanosockets", EntryPoint = "nanosockets_bind", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Bind(Socket socket, IntPtr address);

  [DllImport("nanosockets", EntryPoint = "nanosockets_bind", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Bind(Socket socket, ref Address address);

  [DllImport("nanosockets", EntryPoint = "nanosockets_connect", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Connect(Socket socket, ref Address address);

  [DllImport("nanosockets", EntryPoint = "nanosockets_set_option", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetOption(
    Socket socket,
    int level,
    int optionName,
    ref int optionValue,
    int optionLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_get_option", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetOption(
    Socket socket,
    int level,
    int optionName,
    ref int optionValue,
    ref int optionLength);

  public static Status SetNonBlocking(Socket socket, bool shouldBlock = false)
  {
    return UDP.SetNonBlocking(socket, shouldBlock ? (byte) 0 : (byte) 1);
  }

  [DllImport("nanosockets", EntryPoint = "nanosockets_set_nonblocking", CallingConvention = CallingConvention.Cdecl)]
  private static extern Status SetNonBlocking(Socket socket, byte state);

  [DllImport("nanosockets", EntryPoint = "nanosockets_set_dontfragment", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetDontFragment(Socket socket);

  [DllImport("nanosockets", EntryPoint = "nanosockets_poll", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Poll(Socket socket, long timeout);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(Socket socket, IntPtr address, IntPtr buffer, int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(Socket socket, IntPtr address, byte[] buffer, int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(
    Socket socket,
    ref Address address,
    IntPtr buffer,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(
    Socket socket,
    ref Address address,
    byte[] buffer,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send_offset", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(
    Socket socket,
    IntPtr address,
    byte[] buffer,
    int offset,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_send_offset", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Send(
    Socket socket,
    ref Address address,
    byte[] buffer,
    int offset,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(Socket socket, IntPtr address, IntPtr buffer, int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(Socket socket, IntPtr address, byte[] buffer, int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(
    Socket socket,
    ref Address address,
    IntPtr buffer,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(
    Socket socket,
    ref Address address,
    byte[] buffer,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive_offset", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(
    Socket socket,
    IntPtr address,
    byte[] buffer,
    int offset,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_receive_offset", CallingConvention = CallingConvention.Cdecl)]
  public static extern int Receive(
    Socket socket,
    ref Address address,
    byte[] buffer,
    int offset,
    int bufferLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_get", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetAddress(Socket socket, ref Address address);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_is_equal", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status IsEqual(ref Address left, ref Address right);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_set_ip", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetIP(ref Address address, IntPtr ip);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_set_ip", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetIP(ref Address address, string ip);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_get_ip", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetIP(ref Address address, IntPtr ip, int ipLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_get_ip", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetIP(ref Address address, StringBuilder ip, int ipLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_set_hostname", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetHostName(ref Address address, IntPtr name);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_set_hostname", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status SetHostName(ref Address address, string name);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_get_hostname", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetHostName(ref Address address, IntPtr name, int nameLength);

  [DllImport("nanosockets", EntryPoint = "nanosockets_address_get_hostname", CallingConvention = CallingConvention.Cdecl)]
  public static extern Status GetHostName(ref Address address, StringBuilder name, int nameLength);
}
